# Maintainer: David Runge <dvzrv@archlinux.org>

pkgbase=libtraceevent
pkgname=(
  libtraceevent
  libtraceevent-docs
)
pkgver=1.7.2
pkgrel=1
epoch=1
pkgdesc="Linux kernel trace event library"
arch=(loong64 x86_64)
url="https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/about/"
license=(GPL2 LGPL2.1)
makedepends=(
  asciidoc
  xmlto
)
source=(
  $pkgname-$pkgver.tar.gz::https://git.kernel.org/pub/scm/libs/libtrace/$pkgname.git/snapshot/$pkgname-$pkgver.tar.gz
  $pkgname-1.5.0-documentation.patch
)
sha512sums=('aa6d14e6d2e8e64c7d53ba9f93d47331d5f1b5a5a624891ec18a08b0d4e3e82f7845d29957bb3fe4360151bb5870a67c69d5cb1f758bb2956d946221f186546e'
            '74b34a722d3fb9d672826e0b6b137f94a7fcd41bb372f8944bb6a0c652c0e725aaef8e95284fc091c2e9954bcf1656b5428d7e0f121682d40c25623178ee4a1e')
b2sums=('03eae9ea7a8cdf6b015af95031d6fbe8638361f8b0db836f48e9ef47c814465fad583440f80f0103e08758712bd24f7562e1a8678ab24d4a1749a0b88efa7a85'
        'f66f4f20dfe562407a0d4bb53785d1515c4b5f4be64dd96e06ea6c9e03c299b2f0f613901c30893a976b3874d13e768791632a7cb89be92f3788330e4f5ab97d')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  patch -d $pkgname-$pkgver -p1 -i ../$pkgname-1.5.0-documentation.patch
}

build() {
  make -C $pkgname-$pkgver
  make -C $pkgname-$pkgver/Documentation
}

package_libtraceevent() {
  depends=(glibc)
  optdepends=('libtraceevent-docs: for documentation')
  provides=(libtraceevent.so)

  make libdir_relative=lib prefix=/usr DESTDIR="$pkgdir/" install -C $pkgname-$pkgver
  make libdir_relative=lib prefix=/usr DESTDIR="$pkgdir/" install -C $pkgname-$pkgver/Documentation
  (
    cd "$pkgdir"
    _pick libtraceevent-docs usr/share/doc
  )
}

package_libtraceevent-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
}
